//
//  LXPermissionMedia.swift
//  LXPermissionAuth
//
//  Created by yoctech on 2021/8/18.
//

import UIKit
import MediaPlayer

@objc public class LXPermissionMedia: NSObject, LXPermissionProtocol {
    @objc public static func auth(autoRequestIfNot: Bool, clouser: @escaping LXAuthClouser) {
        let aStatus = MPMediaLibrary.authorizationStatus()
        switch aStatus {
        /// 未作出选择
        case .notDetermined:
            if autoRequestIfNot {
                DispatchQueue.main.async {
                    MPMediaLibrary.requestAuthorization { (status) in
                        if status == .authorized{
                            clouser(true, .authorized)
                        }else{
                            clouser(false, .other)
                        }
                    }
                }
            }else {
                clouser(false, .notDetermined)
            }
        /// 用户明确拒绝此应用程序的授权，或在设置中禁用该服务。
        case .denied:
            clouser(false, .denied)
            
        /// 该应用程序未被授权使用该服务。由于用户无法改变对该服务的主动限制。此状态，并且个人可能没有拒绝授权。
        case .restricted:
            clouser(false, .restricted)
        /// 已授权
        case .authorized:
            clouser(true, .authorized)
        /// 扩展以后可能有的状态,做保护措施
        @unknown default:
            clouser(false, .other)
        }

    }
}
